﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Bai1.Controllers
{
    public class AjaxController : Controller
    {
        //
        // GET: /Ajax/
       
        [HandleError(ExceptionType = typeof(System.Data.DataException), View="DataBaseError")]
        
        public ContentResult GetData(int page, int rows, string sord)
        {
            
            DataTable tb = CreateTable();
            int nTotal = 0;
            int nPage = 1;
            for (int i = 0; i < 100; i++)
            {
                tb.Rows.Add((i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1), (i + 1));
            }
            nTotal = tb.Rows.Count;
            StringBuilder sData = new StringBuilder();
            string sTable = Newtonsoft.Json.JsonConvert.SerializeObject(tb);
            sData.Append("{");
            sData.Append("page:");
            sData.Append(page);
            sData.Append(",");
            sData.Append("total:");
            sData.Append(nPage);
            sData.Append(",");
            sData.Append("records:");
            sData.Append(nTotal);
            sData.Append(",");
            sData.Append("rows:");
            sData.Append(sTable);
            sData.Append("}");
            return Content(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()).ToString());
        }

        private static DataTable CreateTable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("BKS", typeof(string));
            tb.Columns.Add("LHHD", typeof(string));
            tb.Columns.Add("DVKD", typeof(string));
            tb.Columns.Add("HTTX", typeof(string));
            tb.Columns.Add("GPLX", typeof(string));
            tb.Columns.Add("NGAYHD", typeof(string));
            tb.Columns.Add("TKM", typeof(double));
            tb.Columns.Add("SL3", typeof(int));
            tb.Columns.Add("SL4", typeof(int));
            tb.Columns.Add("SL5", typeof(int));
            tb.Columns.Add("Note", typeof(string));
            tb.Dispose();
            return tb;
        }
    }
}
